//
//  JSSearchLabelView.m
//  JSBasicFramework
//
//  Created by Apple on 2018/9/12.
//  Copyright © 2018年 Apple. All rights reserved.
//

#import "JSSearchLabelView.h"

@interface JSSearchLabelView ()

@property (nonatomic, strong) UIScrollView *contenScrollView;
@property (nonatomic, strong) QMUIFloatLayoutView *historyLayoutView;
@property (nonatomic, strong) QMUIFloatLayoutView *hotLayoutView;

@property (nonatomic, strong) UILabel *historyL;
@property (nonatomic, strong) UILabel *hotL;
@property (nonatomic, strong) QMUIButton *clearBtn;
@property (nonatomic, strong) UILabel *placeholderL;


@end

@implementation JSSearchLabelView
- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        [self didInitialize];
    }
    return self;
}
-(void)didInitialize{
    
    self.contenScrollView = [[UIScrollView alloc] initWithFrame:CGRectZero];
    self.contenScrollView.bounces = NO;
    [self addSubview:self.contenScrollView];
    
    self.historyLayoutView = [[QMUIFloatLayoutView alloc] init];
    self.historyLayoutView.padding = UIEdgeInsetsMake(12, 12, 12, 12);
    self.historyLayoutView.itemMargins = UIEdgeInsetsMake(0, 0, 10, 10);
    self.historyLayoutView.minimumItemSize = CGSizeMake(40, 29);// 以2个字的按钮作为最小宽度
//    self.historyLayoutView.layer.borderWidth = PixelOne;
//    self.historyLayoutView.layer.borderColor = UIColorSeparator.CGColor;
    [self.contenScrollView addSubview:self.historyLayoutView];
    
    self.hotLayoutView = [[QMUIFloatLayoutView alloc] init];
    self.hotLayoutView.padding = UIEdgeInsetsMake(12, 12, 12, 12);
    self.hotLayoutView.itemMargins = UIEdgeInsetsMake(0, 0, 10, 10);
    self.hotLayoutView.minimumItemSize = CGSizeMake(40, 29);// 以2个字的按钮作为最小宽度
//    self.hotLayoutView.layer.borderWidth = PixelOne;
//    self.hotLayoutView.layer.borderColor = UIColorSeparator.CGColor;
    [self.contenScrollView addSubview:self.hotLayoutView];
    
    self.historyL = [UILabel new];
    self.historyL.textAlignment = NSTextAlignmentLeft;
    self.historyL.font = UIFontMake(14);
    self.historyL.textColor = [UIColor blackColor];
    self.historyL.text = @"历史搜索";
    [self.historyL sizeToFit];
    [self.contenScrollView addSubview:self.historyL];
    
    self.hotL = [UILabel new];
    self.hotL.textAlignment = NSTextAlignmentLeft;
    self.hotL.font = UIFontMake(14);
    self.hotL.textColor = [UIColor blackColor];
    self.hotL.text = @"热门搜索";
    [self.hotL sizeToFit];
    [self.contenScrollView addSubview:self.hotL];
    
    self.clearBtn = [QMUIButton buttonWithType:UIButtonTypeCustom];
    [self.clearBtn setTitle:@"清空" forState:UIControlStateNormal];
    self.clearBtn.titleLabel.font = UIFontMake(13);
    [self.contenScrollView addSubview:self.clearBtn];
    [self.clearBtn addTarget:self action:@selector(clearClick) forControlEvents:UIControlEventTouchUpInside];
    
    self.placeholderL = [UILabel new];
    self.placeholderL.font = UIFontMake(13);
    self.placeholderL.textAlignment = NSTextAlignmentCenter;
    self.placeholderL.textColor = [UIColor lightGrayColor];
    self.placeholderL.text = @"还没有搜索历史";
}
-(void)clearClick{
    [self clearanceHistory];
}
- (CGSize)sizeThatFits:(CGSize)size{
    return [self layoutSubviewsWithSize:size shouldLayout:NO];
}
// 设置子视图frame 和 scrollView的contenSize
- (CGSize)layoutSubviewsWithSize:(CGSize)size shouldLayout:(BOOL)shouldLayout{
    CGSize historSize = [self.historyLayoutView sizeThatFits:size];
    CGSize hotSize    = [self.hotLayoutView sizeThatFits:size];
    self.historyL.frame = CGRectMake(0, 0, self.historyL.size.width, self.historyL.size.height);
    self.clearBtn.frame = CGRectMake(size.width - 60, self.historyL.frame.origin.y+self.historyL.frame.size.height - 20, 60, 30);
    self.historyLayoutView.frame = CGRectMake(self.historyL.frame.origin.x, self.clearBtn.frame.origin.y + self.clearBtn.frame.size.height, historSize.width, historSize.height);
    self.hotL.frame   = CGRectMake(self.historyL.frame.origin.x,  self.historyLayoutView.frame.origin.y+ self.historyLayoutView.frame.size.height, self.hotL.size.width, self.hotL.size.height);
    self.hotLayoutView.frame = CGRectMake(self.historyLayoutView.frame.origin.x, self.hotL.frame.origin.y+self.hotL.frame.size.height, hotSize.width, hotSize.height);
    self.contenScrollView.contentSize = CGSizeMake(historSize.width, self.hotLayoutView.frame.origin.y+self.hotLayoutView.frame.size.height);
    
    if (!self.historyArrray.count) {
        self.placeholderL.frame = CGRectMake(0, self.historyLayoutView.frame.origin.y, size.width, self.historyLayoutView.frame.size.height);
        self.placeholderL.hidden = NO;
        [self.contenScrollView addSubview:self.placeholderL];
    }else{
        self.placeholderL.hidden = YES;
        [self.placeholderL removeFromSuperview];
    }
    return self.contenScrollView.contentSize;
}

- (void)layoutSubviews{
    [super layoutSubviews];
    [self layoutSubviewsWithSize:self.size shouldLayout:YES];
    _contenScrollView.frame = self.bounds;
    self.contenScrollView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
    
}
-(void)setHistoryArrray:(NSArray<id<JSSearchLabelProtcol>> *)historyArrray{
    _historyArrray = historyArrray;
    [self.historyLayoutView removeAllSubviews];
    for (int i = 0; i < historyArrray.count; i++) {
        id<JSSearchLabelProtcol>obj = historyArrray[i];
        // 添加视图
        QMUIGhostButton *button = [[QMUIGhostButton alloc] init];
        button.ghostColor = [UIColor lightGrayColor];
        [button setTitle:obj.titleString forState:UIControlStateNormal];
        button.titleLabel.font = UIFontMake(14);
        button.contentEdgeInsets = UIEdgeInsetsMake(6, 20, 6, 20);
        [self.historyLayoutView addSubview:button];
        button.tag = i;
        [button addTarget:self action:@selector(historyClick:) forControlEvents:UIControlEventTouchUpInside];
        
    }
    
    [self setNeedsLayout];
    [self layoutIfNeeded];
}
- (void)setHotArray:(NSArray<id<JSSearchLabelProtcol>> *)hotArray{
    _hotArray = hotArray;
    [self.hotLayoutView removeAllSubviews];
    for (int i = 0; i < hotArray.count; i++) {
        id<JSSearchLabelProtcol>obj = hotArray[i];
        QMUIGhostButton *button = [[QMUIGhostButton alloc] init];
        button.ghostColor = [UIColor lightGrayColor];
        [button setTitle:obj.titleString forState:UIControlStateNormal];
        button.titleLabel.font = UIFontMake(14);
        button.contentEdgeInsets = UIEdgeInsetsMake(6, 20, 6, 20);
        [self.hotLayoutView addSubview:button];
        button.tag = i;
        [button addTarget:self action:@selector(hotClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    [self setNeedsLayout];
     [self layoutIfNeeded];
}
-(void)historyClick:(UIButton *)sender {
    if (self.DidSelectItem) {
        self.DidSelectItem(self.historyArrray[sender.tag]);
    }
    if ([self.delegate respondsToSelector:@selector(didSelectItem:)]) {
        [self.delegate didSelectItem:self.historyArrray[sender.tag]];
    }
}
-(void)hotClick:(UIButton *)sender {
    if (self.DidSelectItem) {
        self.DidSelectItem(self.hotArray[sender.tag]);
    }
    if ([self.delegate respondsToSelector:@selector(didSelectItem:)]) {
        [self.delegate didSelectItem:self.hotArray[sender.tag]];
    }
}
- (void)setNomarlColor:(UIColor *)nomarlColor{
    _nomarlColor = nomarlColor;
    
    for (QMUIGhostButton *button in self.historyLayoutView.subviews) {
        button.ghostColor = nomarlColor;
    }
    for (QMUIGhostButton *button in self.hotLayoutView.subviews) {
        button.ghostColor = nomarlColor;
    }
}
-(void)appendHistorys:(NSArray <id<JSSearchLabelProtcol>>*)objs{
    NSMutableArray *ar = @[].mutableCopy;
    [ar addObjectsFromArray:self.historyArrray];
    [ar addObjectsFromArray:objs];
    self.historyArrray = [ar copy];
}
-(void)appendHots:(NSArray <id<JSSearchLabelProtcol>>*)objs{
    // 在添加时缓存记录
    for (id<JSSearchLabelProtcol>obj in objs) {
        JSHistoryModel *m = [JSHistoryModel new];
        m.key = obj.titleString;
        m.ID = obj.mark;
        [m save];
    }
    
    NSMutableArray *ar = @[].mutableCopy;
    [ar addObjectsFromArray:self.hotArray];
    [ar addObjectsFromArray:objs];
    self.historyArrray = [ar copy];
}

-(void)clearanceHistory{
    [JSHistoryModel clearTable];
    self.historyArrray = nil;
    
}
-(void)loadHistoryData{
    NSArray *ar = [JSHistoryModel findAll];
    [self appendHistorys:ar];
}

@end
